define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, undefined, Echarts, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格參數配置
            Table.api.init({
                extend: {
                    index_url: 'aps/procedure/index',
                    add_url: '',
                    edit_url: 'aps/procedure/edit',
                    del_url: '',
                    multi_url: 'aps/procedure/multi',
                    table: 'aps_procedure',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                showToggle: false,
                sortName: 'id',
                searchFormVisible: true,
                showToggle: false,
                showColumns: false,
                showExport: false,
                search: false,
                dblClickToEdit: false, //是否啟用雙擊編輯
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'mainprocess.name', title: __('Mainprocess.name')},
                        {field: 'devicetype.code', title: __('代號')},
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        //{field: 'number', title: __('Number')},
                        {field: 'r0', title: __('最新'), operate: false},
                        {field: 'r1', title: __('紀錄1'), operate: false},
                        {field: 'r2', title: __('紀錄2'), operate: false},
                        {field: 'r3', title: __('紀錄3'), operate: false},
                        {field: 'r4', title: __('紀錄4'), operate: false},
                        {
                            field: 'manhours',
                            title: __('Manhours'),
                            editable: {
                                emptytext: '--',
                                success: function (response, newValue) {
                                    //window.location.reload();
                                }
                            },
                            operate:false
                        },
                        {field: 'maxhours', title: __('最長工時'), operate:false},
                        //{field: 'lydays', title: __('來料採購最長天數'), operate:false},
                        //{field: 'state', title: __('State'), searchList: {"0":__('State 0'),"1":__('State 1')}, formatter: Table.api.formatter.normal},
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'update_time', title: __('Update_time'), operate:false},
                        //{field: 'devicetype.id', title: __('Devicetype.id')},
                        //{field: 'devicetype.create_time', title: __('Devicetype.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'devicetype.update_time', title: __('Devicetype.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        ////{field: 'mainprocess.id', title: __('Mainprocess.id')},
                        //{field: 'mainprocess.number', title: __('Mainprocess.number')},
                        //{field: 'mainprocess.manhours', title: __('Mainprocess.manhours'), operate:'BETWEEN'},
                        //{field: 'mainprocess.state', title: __('Mainprocess.state')},
                        //{field: 'mainprocess.create_time', title: __('Mainprocess.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'mainprocess.update_time', title: __('Mainprocess.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    title: __('工作記錄'),
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa-hand-o-right',
                                    url: 'aps/statistics/index'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 為表格綁定事件
            Table.api.bindevent(table);
        },
        zcgztj: function () {
            Controller.bingtu();
            Controller.tiaotu();

            //提交只能POST，
            //不submit，自己跳转
            $(document).on("click", "#dosearch", function () {
                var url = 'zcgztj?';
                var formdata = $("#a_form").serialize();
                //console.log(formdata);
                //Backend.api.ajax({
                //    url: url,
                //    data: formdata
                //}, function (data, ret) {
                //    console.log(data);
                //    console.log(ret.url);
                //    //window.parent.location.href = ret.url;
                //});
                //window.parent.location.href = url + formdata;
                self.window.open(url + formdata);
                return false;
            });


            Controller.api.bindevent(); //只能自己提交
        },
        bingtu: function() {
            var myChart = Echarts.init(document.getElementById('echart1'), 'walden');

            var data = genData(50);

            var option = {
                title: {
                    text: '總工時: ' + Statsdata.totlgs + " 小時",
                    subtext: '',
                    left: 'left'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b} : {c} ({d}%)'
                },
                legend: {
                    type: 'scroll',
                    orient: 'vertical',
                    right: 80,
                    top: 20,
                    bottom: 20,
                    data: data.legendData,

                    selected: data.selected
                },
                series: [
                    {
                        name: '製程',
                        type: 'pie',
                        radius: '55%',
                        center: ['40%', '50%'],
                        data: data.seriesData,
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };

            function genData(count) {

                var selected = {};
                for (var i = 0; i < Statsdata.legendData.length; i++) {
                    var name = Statsdata.legendData[i];
                    selected[name] = false;
                    if (Statsdata.seriesData[i].value > 0) {
                        //console.log(Statsdata.seriesData[i].value);
                        selected[name] = true;
                    }
                }
                //console.log(Statsdata.seriesData);
                //console.log(selected);
                return {
                    legendData: Statsdata.legendData,
                    seriesData: Statsdata.seriesData,
                    selected: selected
                };
            }

            myChart.setOption(option);
        },
        tiaotu: function() {
            var myChart2 = Echarts.init(document.getElementById('echart2'), 'walden');

            var dataAxis = Statsdata.legendData;
            var yMax = 0;
            var data = new Array();

            for (var i = 0; i < Statsdata.seriesData.length; i++) {
                data.push(Statsdata.seriesData[i].value);
                if (data[i] > yMax) {
                    yMax = data[i];
                }
            }

            yMax = parseInt(yMax/10 * 10);

            var dataShadow = [];

            for (var i = 0; i < data.length; i++) {
                dataShadow.push(yMax);
            }

            var option2 = {
                title: {
                    text: '',
                    subtext: ''
                },
                xAxis: {
                    data: dataAxis,
                    axisLabel: {
                        inside: true,
                        textStyle: {
                            color: '#000000',
                            fontSize: 7
                        }
                    },
                    axisTick: {
                        show: false
                    },
                    axisLine: {
                        show: false
                    },
                    z: 10
                },
                yAxis: {
                    axisLine: {
                        show: false
                    },
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        textStyle: {
                            color: '#999'
                        }
                    }
                },
                dataZoom: [
                    {
                        type: 'inside'
                    }
                ],
                series: [
                    { // For shadow
                        type: 'bar',
                        itemStyle: {
                            color: 'rgba(0,0,0,0.05)'
                        },
                        barGap: '-100%',
                        barCategoryGap: '40%',
                        data: dataShadow,
                        animation: false
                    },
                    {
                        type: 'bar',
                        itemStyle: {
                            color: new Echarts.graphic.LinearGradient(
                                0, 0, 0, 1,
                                [
                                    {offset: 0, color: '#83bff6'},
                                    {offset: 0.5, color: '#188df0'},
                                    {offset: 1, color: '#188df0'}
                                ]
                            )
                        },
                        emphasis: {
                            itemStyle: {
                                color: new Echarts.graphic.LinearGradient(
                                    0, 0, 0, 1,
                                    [
                                        {offset: 0, color: '#2378f7'},
                                        {offset: 0.7, color: '#2378f7'},
                                        {offset: 1, color: '#83bff6'}
                                    ]
                                )
                            }
                        },
                        data: data
                    }
                ]
            };

// Enable data zoom when user click bar.
            var zoomSize = 6;
            myChart2.on('click', function (params) {
                console.log(dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)]);
                myChart2.dispatchAction({
                    type: 'dataZoom',
                    startValue: dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)],
                    endValue: dataAxis[Math.min(params.dataIndex + zoomSize / 2, data.length - 1)]
                });
            });

            myChart2.setOption(option2);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});